package project.background.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fld.background.service.UserService;
import fld.background.system.service.impl.BaseServiceImpl;
import fld.utils.Common;
import fld.utils.DataGrid;
import fld.utils.Page;
import fld.utils.Result;
import project.background.dto.TeamMemberDto;
import project.background.entity.TeamMember;

/**
 * 企业团队成员
 * @author yc
 * 2019年1月11日
 */
@Service
public class TeamMemberService extends BaseServiceImpl<TeamMember,Long>{
	
	@Autowired
	private UserService userService;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	private SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd");
	
	/**
	 * 查询分页
	 * @param tmap
	 * @return
	 */
	public DataGrid grid(Map<String,Object> tmap){
		if(!"".equals(Common.nullToEmpty(tmap.get("title")))){
			tmap.put("title","%"+tmap.get("title")+"%");
		}
		if("".equals(Common.nullToEmpty(tmap.get("companyId")))) {
			tmap.put("companyId", userService.getCurUser().getCompanyId());
		}else {
			tmap.put("companyId", Long.parseLong(tmap.get("companyId").toString()));
		}
		Page<TeamMember> page = queryForPage(TeamMemberDto.class, tmap);
		List<Map<String,Object>> rows = new ArrayList<Map<String,Object>>();
		List<TeamMember> res = page.getRows();
		for (TeamMember t : res) {
			Map<String,Object> m = new HashMap<String,Object>();
			m.put("id", t.getId());
			m.put("createTime", null == t.getCreateTime()?"":sdf.format(t.getCreateTime()));
			m.put("title", t.getTitle());
			m.put("publishDate", sdfd.format(t.getPublishDate()));
			m.put("jjtp", t.getJjtp());
			m.put("firstjjtp", "");
			if(!"".equals(Common.nullToEmpty(t.getJjtp()))) {
				m.put("firstjjtp", t.getJjtp().split(",")[0]);
			}
			m.put("brief", t.getBrief());
			m.put("companyId", t.getCompanyId());
			m.put("pv", t.getPv());
			rows.add(m);
		}
		DataGrid dg = new DataGrid();
		dg.setTotal(page.getTotal());
		dg.setRows(rows);
		dg.setTotalPage(page.getPageCount());
		return dg;
	}
	
	/**
	 * 获取详情
	 * @param id
	 * @return
	 */
	public Map<String,Object> getInfo(Long id){
		TeamMember t = getDao().get(id);
		Map<String,Object> m = new HashMap<String,Object>();
		if(t != null){
			m.put("id", t.getId());
			m.put("createTime", null == t.getCreateTime()?"":sdf.format(t.getCreateTime()));
			m.put("title", t.getTitle());
			m.put("publishDate", sdfd.format(t.getPublishDate()));
			m.put("jjtp", t.getJjtp());
			m.put("brief", t.getBrief());
			m.put("details", t.getDetails());
			m.put("companyId", t.getCompanyId());
			m.put("pv", t.getPv());
			addPv(id);
		}
		return m;
	}
	
	/**
	 * 新增/修改
	 * @param t
	 */
	public Result saveObj(TeamMember t) {
		if("".equals(Common.nullToEmpty(t.getId()))){
			//新增
			Long companyId = userService.getCurUser().getCompanyId();
			if(null  == companyId) {
				return new Result(false,"没有操作权限");
			}else {
				t.setCompanyId(companyId);
			}
			t.setPv(0);
			save(t);
		}else{
			
			TeamMember old = getDao().get(t.getId());
			old.setTitle(t.getTitle());
			old.setPublishDate(t.getPublishDate());
			old.setJjtp(t.getJjtp());
			old.setBrief(t.getBrief());
			old.setDetails(t.getDetails());
			save(old);
		}
		return new Result(true);
	}
	
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	public Result deleteById(Long id){
		TeamMember t = getDao().get(id);
		if(null != t) {
			t.setIsdeleted(true);
			save(t);
		}
		return new Result(true);
	}
	
	/**
	 * 增加pv
	 * @param id
	 * @return
	 * 2019年1月12日
	 * @author yc
	 */
	public Result addPv(Long id){
		TeamMember t = getDao().get(id);
		if(null != t) {
			t.setPv(t.getPv()+1);
			save(t);
		}
		return new Result(true);
	}
	
}
